#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:HeWenjun
# datetime:2023/11/9 15:16
# software: PyCharm
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import json

import acceptance_test
import apply_online
import security_scan
import submit_to_tester


def main_sender(subject, html_content):
    # 发件人邮箱的SMTP服务器地址
    smtp_server = 'mail.mochasoft.com.cn'
    smtp_port = 25  # SMTP服务器端口号

    # 发件人邮箱的登录信息
    sender_email = 'hewj@mochasoft.com.cn'
    sender_password = 'F6bVlc3KDG4mlm23'

    # 收件人邮箱地址（可以是一个字符串或以列表形式存储多个收件人）
    to_email = 'hewj@mochasoft.com.cn'
    # cc_email = 'cc@example.com' # 抄送人

    # 创建包含HTML正文的MIMEMultipart对象
    message = MIMEMultipart('alternative')
    message['Subject'] = subject
    message['From'] = sender_email
    message['To'] = to_email
    # message['Cc'] = cc_email

    # 将HTML内容转换为MIMEText对象并添加到MIMEMultipart对象中作为正文部分
    html_part = MIMEText(html_content, 'html')
    message.attach(html_part)

    try:
        # 连接SMTP服务器并进行身份验证（如果需要TLS/SSL，请在connect之后调用starttls方法）
        smtp_obj = smtplib.SMTP(smtp_server, smtp_port)
        # smtp_obj.starttls()  # 如果需要TLS/SSL，请取消注释此行
        smtp_obj.login(sender_email, sender_password)

        # 发送邮件
        smtp_obj.sendmail(sender_email, to_email.split(','), message.as_string())
        # smtp_obj.sendmail(sender_email, to_email.split(',') + cc_email.split(','), message.as_string())
        print(subject + '--邮件发送成功')

    except smtplib.SMTPException as e:
        print('邮件发送失败:', e)

    finally:
        # 关闭连接
        smtp_obj.quit()


# 邮件中，随版本变化的信息从version_info.json中读取
def assemble_the_data():
    file_paths = ['version_info.json', 'website.json']

    # 存储每个文件的内容的字典
    file_contents = {}

    for file_path in file_paths:
        with open(file_path, 'r', encoding='utf-8') as f:
            content_dict = f.read()
            file_contents[file_path] = json.loads(content_dict)
    system_name = file_contents['version_info.json']['name']
    system_version = file_contents['version_info.json']['version']
    system_description = file_contents['version_info.json']['description']
    website = file_contents['website.json'][system_name]
    return system_name, system_version, system_description, website


if __name__ == '__main__':
    system_info = assemble_the_data()

    submit = submit_to_tester.assemble_the_body(name=system_info[0], version=system_info[1],
                                                description=system_info[2], website=system_info[3])
    security = security_scan.assemble_the_body(name=system_info[0], version=system_info[1],
                                               description=system_info[2], website=system_info[3])
    online = apply_online.assemble_the_body(name=system_info[0], version=system_info[1],
                                            description=system_info[2])
    acceptance = acceptance_test.assemble_the_body(name=system_info[0], version=system_info[1],
                                                   description=system_info[2], website=system_info[3])
    # 提测
    main_sender(subject=submit[0], html_content=submit[1])
    # 安扫
    main_sender(subject=security[0], html_content=security[1])
    # 上线
    main_sender(subject=online[0], html_content=online[1])
    # 验收报告
    main_sender(subject=acceptance[0], html_content=acceptance[1])
